package com.jzg.jcptadmin.ui;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.VehicleCheckData;
import com.jzg.jcptadmin.presenter.VehicleCheckPresenter;
import com.jzg.jcptadmin.view.FullyGridLayoutManager;
import com.jzg.jcptadmin.viewinterface.VehicleCheckInterface;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jzg on 2015/12/28.
 * 车辆详情查看
 */
public class VehicleCheckActivity extends BaseActivity implements View.OnClickListener, VehicleCheckInterface {
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_operation)
    TextView tvOperation;//操作
    @Bind(R.id.car_style_text_show)
    TextView carStyleTextShow;
    @Bind(R.id.car_time_text_show)
    TextView carTimeTextShow;
    @Bind(R.id.car_place_text_show)
    TextView carPlaceTextShow;
    @Bind(R.id.mileage_danwei_text)
    TextView mileageDanweiText;
    @Bind(R.id.car_color_text_show)
    TextView carColorTextShow;
    @Bind(R.id.car_biaoxin_text_show)
    TextView carBiaoxinTextShow;
    @Bind(R.id.car_nianjian_text_show)
    TextView carNianjianTextShow;
    @Bind(R.id.car_kaipiao_text)
    TextView carKaipiaoText;
    @Bind(R.id.kaipiao_danwei_text)
    TextView kaipiaoDanweiText;
    @Bind(R.id.car_beizhu_text)
    TextView carBeizhuText;
    @Bind(R.id.car_beizhu_text_show)
    TextView carBeizhuTextShow;
    @Bind(R.id.pic_gridview)
    RecyclerView picGridview;
    @Bind(R.id.tv_fabufangshi)
    TextView tvFabufangshi;

    private PopupWindow pwMyPopWindow;
    private ListView lvPopupList;
    private ArrayList<Map<String, String>> moreList;
    private int NUM_OF_VISIBLE_LIST_ROWS = 3;// 指定popupwindow中Item的数量
    private VehicleCheckData mData;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vehiclecheck);
        ButterKnife.bind(this);
        String Itemid = getIntent().getStringExtra(Constant.ACTIVITY_CARLISTID);
        if (Itemid != null) {
            VehicleCheckPresenter vehicleCheckPresenter = new VehicleCheckPresenter(DataManager.getInstance());
            vehicleCheckPresenter.attachView(this);
            vehicleCheckPresenter.LoadCarData(appContext.getUser().getUserId() + "", Itemid);
        }
       // initData();
        initView();
        iniPopupWindow();


    }

    private void initData() {
      /*  moreList = new ArrayList<Map<String, String>>();
        Map<String, String> map;
        map = new HashMap<String, String>();
        map.put("share_key", "刷新");
        moreList.add(map);
        map = new HashMap<String, String>();
        map.put("share_key", "修改");
        moreList.add(map);
        map = new HashMap<String, String>();
        map.put("share_key", "保存");
        moreList.add(map);*/

    }
    public void InitView() {
        carStyleTextShow.setText(mData.getFullName());//车型车系
        carTimeTextShow.setText(mData.getRegDateYear() + "-" + mData.getRegDateMonth());
        carPlaceTextShow.setText(mData.getProvinceName() + " " + mData.getCityName());//上牌地区
        mileageDanweiText.setText(mData.getMileage());//行驶公里
        carColorTextShow.setText(mData.getColorName());
        // carBiaoxinTextShow.setText(mData.get);//保险到期
        kaipiaoDanweiText.setText(mData.getNowMsrp());
        tvFabufangshi.setText(mData.getPublishAreaName());
        carBeizhuTextShow.setText(mData.getDes());


    }

    private void initView() {

        FullyGridLayoutManager gridLayoutManager = new FullyGridLayoutManager(this, 3);
        picGridview.setLayoutManager(gridLayoutManager);
       /* PicCheckAdapter adapter = new PicCheckAdapter(this);
        picGridview.setAdapter(adapter);
        adapter.setOnItemClickLitener(
                new RecycleViewBaseAdapter.OnItemClickLitener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Intent intent = new Intent(VehicleCheckActivity.this, PhotoActivity.class);
                        intent.putExtra("position", position);
                        startActivity(intent);
                    }
                }
        );*/

        tvOperation.setOnClickListener(this);
    }

    private void iniPopupWindow() {

        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.task_detail_popupwindow, null);
        lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
        pwMyPopWindow = new PopupWindow(layout);
        pwMyPopWindow.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件
        lvPopupList.setAdapter(new SimpleAdapter(VehicleCheckActivity.this, moreList,
                R.layout.list_item_popupwindow, new String[]{"share_key"},
                new int[]{R.id.tv_list_item}));
        lvPopupList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                Toast.makeText(VehicleCheckActivity.this,
                        moreList.get(position).get("share_key"),
                        Toast.LENGTH_LONG).show();
                if (pwMyPopWindow.isShowing()) {

                    pwMyPopWindow.dismiss();// 关闭
                } else {

                    pwMyPopWindow.showAsDropDown(tvOperation);// 显示
                }
            }
        });

        // 控制popupwindow的宽度和高度自适应
        lvPopupList.measure(View.MeasureSpec.UNSPECIFIED,
                View.MeasureSpec.UNSPECIFIED);
        pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
        /*pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight() + 20)
                * NUM_OF_VISIBLE_LIST_ROWS);*/
        pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight() + 50)
                * NUM_OF_VISIBLE_LIST_ROWS);

        // 控制popupwindow点击屏幕其他地方消失
        pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(
                R.drawable.bg_tanchu));// 设置背景图片，不能在布局中设置，要通过代码来设置
        pwMyPopWindow.setOutsideTouchable(true);// 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_operation:
                if (pwMyPopWindow.isShowing()) {
                    pwMyPopWindow.dismiss();// 关闭
                } else {
                    pwMyPopWindow.showAsDropDown(tvOperation);// 显示
                }
                break;
            default:

                break;
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public void showDatas(VehicleCheckData datas) {
        if (datas.getStatus() == 100) {
            this.mData = datas;
            InitView();
            Logger.e("datas" + datas.toString());

        }else{
            Toast.makeText(VehicleCheckActivity.this, datas.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showError(String error) {
        Toast.makeText(VehicleCheckActivity.this, Constant.errr_net, Toast.LENGTH_SHORT).show();
    }
}
